<?php

// +----------------------------------------------------------------------
// | 权限组控制器
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\store\admin;

use app\model\store\DepositOrder as DepositOrderModel;
use app\event\store\DepositOrder as DepositOrderEvent;
use app\model\ucenter\User as UserModel;
use yicmf\builder\Edit;
use yicmf\builder\Table;
use app\controller\admin\Common;

class DepositOrder extends Common
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function _filter()
    {
        $where = [];
        $field = $this->request->param('field/a');
        if (!isset($field['status'])) {

            $where[] = ['status', '>', 0];
        }
        return $where;
    }

    public function index()
    {
        $statusOptions = [
            [
                'id' => 0,
                'value' => '已取消'
            ],
            [
                'id' => 1,
                'value' => '待支付'
            ],
            [
                'id' => 2,
                'value' => '待发货'
            ],
            [
                'id' => 3,
                'value' => '待确认'
            ],
            [
                'id' => 5,
                'value' => '已完成'
            ]
        ];
        $where = $this->_filter();
        // 显示页面
        $builder = new Table();
        $builder->model(DepositOrderModel::class)
            ->where($where)
            ->order('id DESC')
//                ->suggest('高等级可以对低等级用户进行操作或者授权')
//            ->searchUser('下单用户')
            ->searchSelect('status', '订单状态', $statusOptions)
            ->searchUser('合伙人')
//            ->buttonUpdate('update?type=0')
            ->keyId()
            ->keyText('name', '订单名称')
//            ->keyText('effective_number', '有效POS个数')
//            ->keyText('good_amount', 'POS金额')
            ->keyText('address_info_text', '地址', false, 200)
            ->keyText('amount', '订单金额')
            ->keyText('size_name', '衣服尺码')
            ->keyText('remark', '备注')
//            ->keyText('remark', '备注')
            ->keyUser('user_id', '下单用户')
//            ->keyTime('cancel_time','取消时间')
//				->keyText('description', '描述')
            ->keyCreateTime('下单时间')
            ->keyStatus(['已取消', '待支付', '待发货', '待确认', '已完成', '已完成'])
//            ->quickUpdate(['title','description'])

            ->actionUpdate('send?id={$id}', '发货', [2]);
//            ->actionDelete();
//            ->actionForbid('forbid?id={$id}', '禁用', function ($data) {
//                return 1 == $data['status'] && 1 < $data['id'];
//            });
        return $builder->fetch();
    }

    public function send()
    {
        if ($this->request->isPost()) {
            $event = new DepositOrderEvent();
            $data = $event->send($this->request->post(), $this->user);
            if (0 === $data['code']) {
                $this->_returnSuccess($data['message']);
            } else {
                $this->_returnError($data['message'], false);
            }
        } else {
            // 同步节点与菜单
            if ($this->request->param('id')) {
                $group_id = $this->request->param('id');
                $data = DepositOrderModel::where('id', $group_id)->where('status', 2)->find();
                if (!$data) {
                    $this->_returnError('标签不存在');
                }
            } else {
                $data = [];
            }
            $builder = new Edit();
            $builder->title('发货')
                ->keyRadio('send_type', '发货方式', ['自提', '快递'], null, 0, null, true)
                ->keyText('size_name', '衣服尺码')
                ->keyText('remark', '备注')
                ->keyLabel('address_info_text', '地址', '')
                ->keyTextInline('express_name', '快递公司', '')
                ->keyTextInline('express_sn', '快递单号', '')
                ->setTrigger('send_type', '1', 'express_name,express_sn')
                ->setTrigger('send_type', '0', '')
                ->data($data)
                ->buttonSubmit(null, '确认发货');
            return $builder->fetch();
        }
    }


}
